package com.example.controller;

import com.example.aspect.annotation.AllowAnon;
import com.example.aspect.annotation.Decrypt;
import com.example.aspect.annotation.Encrypt;
import com.example.aspect.annotation.ChineseCheck;
import com.example.constant.TokenConstant;
import com.example.exception.ServiceException;
import com.example.pojo.entity.User;
import com.example.pojo.query.UserQuery;
import com.example.pojo.vo.UserVO;
import com.example.response.AjaxJson;
import com.example.service.RedisService;
import com.example.service.UserService;
import com.example.utils.JsonUtil;
import com.example.utils.JwtUtil;
import com.example.utils.ResultCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理接口")
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @PostMapping("/page")
//    @AllowAnon
    @ApiOperation("根据复杂条件分页查询用户接口")
    public AjaxJson queryUsersPage(@RequestBody UserQuery query) {
        List<UserVO> userListByState = userService.selectUserList(query);
        Map<String, Object> map = new HashMap<>();
        map.put("total", userListByState.size());
        map.put("user", userListByState);
        return AjaxJson.ok(map);
    }

    @PostMapping("/user")
    @AllowAnon
    @ApiOperation("/查询用户")
    public AjaxJson select(@RequestBody UserQuery user) {
        UserVO byId = userService.selectByUserName(user.getUserName());
        return AjaxJson.ok(byId);
    }

    @PostMapping("/login")
    @AllowAnon
    @Decrypt
    @Encrypt
    @ApiOperation("用户登录接口")
    @ChineseCheck(User.class)
    public AjaxJson login(@RequestParam Map<String, String> user) {
        if (user.isEmpty()) {
            return AjaxJson.error(ResultCodeUtil.NULL_ERROR);
        }
        if (user.size() < 2) {
            return AjaxJson.error(ResultCodeUtil.NULL_ERROR, "数据格式不对");
        }
        String userJson = JsonUtil.stringify(user);
        User parse = JsonUtil.parse(userJson, User.class);
        UserVO userVO = userService.selectByUserNameAndPasswd(parse);
        User token_u = new User();
        token_u.setUserName(userVO.getUserName());
        UserVO userVO1 = new UserVO(token_u);
        String stringify = JsonUtil.stringify(userVO1);
        String token = JwtUtil.createToken(stringify);
        Map<String, Object> map = new HashMap<>();
        map.put("user", userVO);
        map.put("token", token);
        redisService.save(TokenConstant.TOKEN_REDIS + "user:" + token_u.getUserName(), token);
        return AjaxJson.ok(map);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    public AjaxJson logout(@RequestBody UserQuery user, @RequestHeader("token") String token) {
        String key = TokenConstant.TOKEN_REDIS + user.getUserName();
        Object o = redisService.get(key);
        if (o != null) {
            redisService.delete(key);
            return AjaxJson.ok();
        }
        return AjaxJson.error(ResultCodeUtil.DATA_ERROR);
    }

    @PostMapping("/register")
    @AllowAnon
    @Decrypt
    @Encrypt
    @ApiOperation("用户注册")
    public AjaxJson registration(@RequestParam Map<String, String> user) {
        if (user.isEmpty()) {
            return AjaxJson.error(ResultCodeUtil.NULL_ERROR);
        }
        if (user.size() < 2) {
            return AjaxJson.error(ResultCodeUtil.NULL_ERROR, "数据格式不对");
        }
        String stringify = JsonUtil.stringify(user);
        User parse = JsonUtil.parse(stringify, User.class);
        userService.insertUser(parse);
        return AjaxJson.ok();
    }

    @PostMapping("/update")
    @Decrypt
    @Encrypt
//    @AllowAnon
    @ApiOperation("用户更新")
    public AjaxJson update(@RequestBody User user) {
        userService.updateUser(user);
        return AjaxJson.ok();

    }

    @PostMapping("/check")
    @ApiOperation("校验Token")
    public AjaxJson check(@RequestBody UserQuery user, @RequestHeader("token") String token) {
        String tokenRedis = (String) redisService.get(TokenConstant.TOKEN_REDIS + "user:" + user.getUserName());
        if (!tokenRedis.equals(token)) {
            throw new ServiceException(ResultCodeUtil.VALIDATION_ERROR, "token异常");
        }
        return AjaxJson.ok();
    }

}
